import { getPmFormUser } from '../../../api/manage.js'
import create from 'dd-store'
import pageStore from '../../../stores/globalStore'

create.Page({
  store: pageStore,
  data: {
    scrollTop: 100,
    pmFormId: '',
    userList: [],
    keyword: '',
    newSearchList: [],
    selectedList: [],
    isLoading: false,
    beforeList: []
  },
  onLoad(options) {
    // console.log(this.$store.data.selectedList);
    this.setData({
      pmFormId: options.pmFormId,
      beforeList: this.$store.data.selectedList
    })
    this.getUserList()
  },
  //根据传进来的ID发起请求获取数据
  getUserList() {
    dd.showLoading({
      content: '加载中'
    })
    let data = { pmFormId: this.data.pmFormId }
    getPmFormUser(data).then((res) => {
      this.data.userList = res.data.pmGroupUserList
      this.data.newSearchList = res.data.pmGroupUserList
      let searchList = [];
      res.data.pmGroupUserList.forEach((item) => {
        searchList.push({
          label: item.pmGroupUserName,
          value: item.pmGroupUserName,
          userDDId: item.pmGroupUserDDId,
          isSelected: false
        });
      })
      this.data.beforeList.forEach((item1) => {
        searchList.forEach((item2) => {
          if (item1.value == item2.value) {
            item2.isSelected = true
            // console.log('进来哦了');
          }
        })
      })
      this.setData({
        userList: res.data.pmGroupUserList,
        newSearchList: searchList
      })
    })
    this.closeLoading(500)
  },
  // 输入框的Change事件函数
  onChange(value, e) {
    let newArr = []
    this.data.userList.forEach((item) => {
      // console.log(item.pmGroupUserName);
      // console.log(value);
      // console.log();
      if (item.pmGroupUserName.indexOf(value) != -1) {
        newArr.push({ label: item.pmGroupUserName, value: item.pmGroupUserName })
      }
      this.$store.data.selectedList.forEach((item1) => {
        newArr.forEach((item2) => {
          if (item1.value == item2.value) {
            item2.isSelected = true
          }
        })
      })
    })

    this.data.selectedList.forEach((item1) => {
      newArr.forEach((item2) => {
        if (item1.value == item2.value) {
          item2.isSelected = true
        }
      })
    })
    this.setData({
      newSearchList: newArr
    })
  },
  // 底部确认按钮点击事件
  handleTap() {
    // 清空store里面共享的数据
    this.$store.data.selectedList = []
    this.$store.data.userOrGroup = 'user'
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    console.log(this.data.value);
    if (pages.length > 1) {
      let prePage = pages[pages.length - 2];
      prePage.changeFormGroup(this.data.value)
      // prePage.changeData(this.data.title);
      dd.navigateBack();
    }

    // console.log(this.data.selectedList);

    // 取出mark页面剩余的数据，首先判断上一次是什么状态，如果是考评组就无需取出数据


    this.$store.data.selectedList = this.data.selectedList
    this.update()
  },
  // 底部取消按钮点击事件
  cancel() {
    this.setData({
      value: ''
    })
    dd.navigateBack();
    // console.log('取消');
  },
  // 列表多选框的Change事件
  checkedChange(value) {
    // console.log(value.detail.value);
    let newArr = []
    console.log('当前考评员工数据1', value.detail.value);
    value.detail.value.forEach((item) => {
      newArr.push({
        label: item,
        value: item,
        isSelected: true,
        userDDId: this.data.userList.filter((itemF) => {
          return itemF.pmGroupUserName == item
        })[0].pmGroupUserDDId
      })
    })

    console.log('当前考评员工数据2', newArr);
    this.setData({
      selectedList: newArr,
    })
  },
  // 关闭加载效果的函数
  closeLoading(tiem) {
    setTimeout(() => {
      dd.hideLoading()
    }, tiem);
  },
  // 
  // onConfirm(value) {
  //   my.alert({
  //     content: value,
  //   });
  // },
  // checkedListChange(value, e) {
  //   console.log(value, e);
  // },
  scroll(e) {
    this.setData({
      scrollTop: e.detail.scrollTop,
    });
  },
});
